package at.ac.lbg.media.vis.client.theme.render
{
	
	import at.ac.lbg.media.vis.client.theme.manager.PetalLibrary;
	import at.ac.lbg.media.vis.client.theme.view.Artwork;
	import at.ac.lbg.media.vis.client.theme.view.Category;
	
	import flare.display.DirtySprite;
	import flare.util.Colors;
	import flare.vis.Visualization;
	import flare.vis.data.DataSprite;
	
	import flash.display.Shape;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.utils.Dictionary;
	
	import mx.collections.ArrayCollection;
	import mx.containers.HBox;
	import mx.controls.List;
	import mx.controls.Text;
	import mx.core.Application;
	import mx.core.ScrollPolicy;
	import mx.core.UIComponent;

	public class CategoryRenderer extends HBox {
		private var petal:Shape;
		private var petalContainer:UIComponent;
		private var headline:Text;
		private var list:List = Application.application.legend;
		private var selectedCats:ArrayCollection = new ArrayCollection();
		
		// COLORS
		private var colorActive:uint = Colors.rgba(145, 80, 0);
		// inactive petals on active icon
		private var colorActiveMedium:uint = Colors.rgba(230, 130, 0);
		// active petal
		private var colorActiveBright:uint = Colors.rgba(255, 180, 80);

		private var colorInactive:uint = Colors.rgba(80, 70, 142);
		private var colorInactiveMedium:uint = Colors.rgba(120, 110, 180);
		private var colorInactiveBright:uint = Colors.rgba(200, 210, 230);
		
		
		private var petalLib:PetalLibrary;
		private var catDict:Dictionary;
		private var vis:Visualization;
		private var active:Boolean;
		
		public function CategoryRenderer() {
			super();
			horizontalScrollPolicy = ScrollPolicy.OFF;
			petalLib = new PetalLibrary(colorActive, colorActiveMedium, colorActiveBright, colorInactive, colorInactiveBright, colorInactiveMedium);
			//vis = Application.application.flareviz.visualization;
			
		}
		
		override protected function createChildren():void {

			petalContainer = new UIComponent();
			petal = new Shape();
			petalContainer.addChild(petal);
			petal.name = "petal";
			petalContainer.width = 15;
			petalContainer.height = 21;
			addChild(petalContainer);
			
			headline = new Text();
			headline.name = "headline";
			headline.mouseChildren = false;
			addChild(headline);
			
			
		}
		
		override protected function commitProperties():void {
			super.commitProperties();
			catDict = Application.application.catDict;
			
			if (data) {
				petal.graphics.clear();
				if (data.isCategory) {
					headline.text = data.name;
					headline.buttonMode = false;
					headline.useHandCursor = false;
					//headline.enabled = false;
				} else {
					addEventListener(MouseEvent.CLICK, onClick);
					headline.text = data.name + " (" + data.countArtworks + "%)";
					headline.buttonMode = true;
					var cat:Category = catDict[data.id] as Category;
					var index:int = cat.iconIndex;
					if (index >= petalLib.petals.length) {
						index = 1;
					}
					
					petalLib.initFlower(new Point(-10, -18), false);
					if (active) {
						petalLib.initPetalLegend(petal, true);
					} else {
						petalLib.initPetalLegend(petal, false);
					}
					
					var render:Function = petalLib.petals.getItemAt(index) as Function;
					render(petal.graphics);
					//petalLib.finishPetal(petal);
					petal.rotation = 180;
				}
				

			}
		}
		
		override public function set data(value:Object):void {
			super.data = value;
			
			if (data) {
				if (data.isCategory) {
					headline.setStyle("fontWeight", "bold");
				} else {
					if (Application.application.categoryManager.selectedCatsDict[data.id] != null) {
						active = true;
						this.setStyle("backgroundColor", Colors.rgba(255, 223, 158));
					} else {
						active = false;
						this.setStyle("backgroundColor", Colors.rgba(255, 255, 255));
					}
					headline.setStyle("fontWeight", "normal");
				}
			}
		}
		
        public function onClick(event:MouseEvent):void {
        	var cat:Category = event.currentTarget.data as Category;
        	var cats:Array = Application.application.legend.selectedItems;
			Application.application.flareviz.visualization.data.visit(function(d:DataSprite):void {
				var aw:Artwork = d as Artwork;
				var isSet:Boolean = true;
				for each (var sc:Category in cats) {
					if (!aw.categoryIds.contains(sc.id)) {
						isSet = false;
						break;
					}
				}
				
				if (isSet) {
					aw.highlightMe = cat.id;
					aw.parent.setChildIndex(aw, aw.parent.numChildren - 1);
				} else {
					aw.highlightMe = -1;
				}
			});
			DirtySprite.renderDirty();
		}
		
		
		private function onMouseOut(event:MouseEvent):void {
			Application.application.flareviz.visualization.data.visit(function(d:DataSprite):void {
				var aw:Artwork = d as Artwork;
				aw.highlightMe = -1;
				aw.dirty();
			});
		}
		
	}
}